package com.cchao.simplelib.http.intercaptor;

import android.util.Base64;

import java.nio.charset.StandardCharsets;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {
    private static final String IV = "d89fb05#$%d4f0AJ";//加密向量，16个字节
    private static final String KEY = "e9c8e87^&*8e26XY";//密钥，16个字节
    private static final String CBC_PKCS_7_PADDING = "AES/CBC/PKCS7PADDING";

    //加密
    public static String encrypt(String src, String key , String iv) throws Exception {
        if (key == null) {
            return null;
        }
        if (key.length() != 16) {
            return null;
        }

        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());

        Cipher cipher = Cipher.getInstance(CBC_PKCS_7_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec , ivParameterSpec);

        byte[] bsContent = Base64.encode(src.getBytes() , Base64.NO_WRAP);
        byte[] encrypted = cipher.doFinal(bsContent);

        //此处使用BASE64做转码功能，同时能起到2次加密的作用。
        return new String(Base64.encode(encrypted, Base64.NO_WRAP));
    }

    public static String encrypt(String src) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());

        Cipher cipher = Cipher.getInstance(CBC_PKCS_7_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec , ivParameterSpec);

        byte[] bsContent = Base64.encode(src.getBytes() , Base64.NO_WRAP);
        byte[] encrypted = cipher.doFinal(bsContent);

        //此处使用BASE64做转码功能，同时能起到2次加密的作用。
        return new String(Base64.encode(encrypted, Base64.NO_WRAP));
    }

    //解密
    public static String decrypt(String src, String key , String iv) throws Exception {
        if (key == null) {
            return null;
        }
        if (key.length() != 16) {
            return null;
        }
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance(CBC_PKCS_7_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, keySpec , ivParameterSpec);
        byte[] bsContent = Base64.decode(src.getBytes() , Base64.NO_WRAP);
        byte[] encrypted = cipher.doFinal(bsContent);
        //此处使用BASE64做转码功能，同时能起到2次加密的作用。
        return new String(Base64.decode(encrypted, Base64.NO_WRAP));
    }

    public static String decrypt(String src) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        Cipher cipher = Cipher.getInstance(CBC_PKCS_7_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, keySpec , ivParameterSpec);
        byte[] bsContent = Base64.decode(src.getBytes() , Base64.NO_WRAP);
        byte[] encrypted = cipher.doFinal(bsContent);
        //此处使用BASE64做转码功能，同时能起到2次加密的作用。
        return new String(Base64.decode(encrypted, Base64.NO_WRAP));
    }
}
